<template>
  <div class="register_container">
    <div class="register_box">
      <!--      登陆表单区域-->
      <el-form ref="registerFormRef" :model="registerForm" :rules="registerFormRules" label-width="0px" class="register_form">
        <!--        用户名-->
        <el-form-item prop="telphone">
          <el-input v-model="registerForm.telphone" prefix-icon="el-icon-user-solid"></el-input>
        </el-form-item>
        <!--        密码-->
        <el-form-item prop="password">
          <el-input v-model="registerForm.password" prefix-icon="el-icon-lock" type="password"></el-input>
        </el-form-item>
        <!--        按钮区域-->
        <el-form-item class="btns" >
          <el-button type="primary" @click="register" prop="register">提交注册</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { Message } from 'element-ui'

export default {
  data () {
    return {
      // 登录表单的数据绑定对象
      registerForm: {
        telphone: '',
        password: ''
      },
      // 表单的验证规则对象
      registerFormRules: {
        // 用户名是否合法
        telphone: [
          { required: true, message: '请输入电话号码', trigger: 'blur' },
          { min: 11, max: 11, message: '手机号码不正确', trigger: 'blur' }
        ],
        // 密码是否合法
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 点击重置按钮 重置登录表单
    resetregisterForm () {
      // console.log(this)
      this.$refs.registerFormRef.resetFields()
    },
    register () {
      this.$refs.registerFormRef.validate(async (valid) => {
        if (!valid) return
        // const result = this.$http.post('user/register', this.registerForm)
        // console.log(result)
        var qs = require('qs')
        const { data: res } = await this.$http.post('user/register', qs.stringify(this.registerForm))
        console.log(res)
        if (res.status !== 'success') return Message.error(res.status)
        await this.$router.push('/login')
      })
    }
  }
}
</script>

<!--样式只在该组件生效-->
<style lang="less" scoped>
.register_container{
  background-color: #2b4b6b;
  height: 100%;
}
.register_box{
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  .avatar_box {
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eeeeee;
    }
  }
}
.register_form{
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
.btns {
  display: flex;
  justify-self: flex-end;
}
</style>
